﻿#请求CAMKE的最小构建版本
cmake_minimum_required(VERSION 3.5)
#设置项目名称
project(PushMessage VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#在MSVC上必须指定 /Zc:__cplusplus 选项
if(MSVC)
	add_compile_options(/Zc:__cplusplus)
    #指定头文件包含目录
    include_directories("D:\\Qt\\6.2.4\\msvc2019_64\\include")
elseif(UNIX)
   set(CMAKE_PREFIX_PATH "/home/jxy/Qt/6.2.4/gcc_64") # Qt Kit Dir
   list(APPEND CMAKE_PREFIX_PATH "/home/jxy/Qt/6.2.4/gcc_64/lib/cmake/Qt6")
endif()

#自动查找头文件路径函数(没有去重)
macro(FIND_INCLUDE_DIR result curdir)  #定义函数,2个参数:存放结果result；指定路径curdir；
    file(GLOB_RECURSE children "${curdir}/*.hpp" "${curdir}/*.h" )	#遍历获取{curdir}中*.hpp和*.h文件列表
    message(STATUS "children= ${children}")								#打印*.hpp和*.h的文件列表
    set(dirlist "")														#定义dirlist中间变量，并初始化
    foreach(child ${children})											#for循环
        string(REGEX REPLACE "(.*)/.*" "\\1" LIB_NAME ${child})			#字符串替换,用/前的字符替换/*h
        if(IS_DIRECTORY ${LIB_NAME})									#判断是否为路径
            LIST(APPEND dirlist ${LIB_NAME})							#将合法的路径加入dirlist变量中
        endif()															#结束判断
    endforeach()														#结束for循环
    set(${result} ${dirlist})											#dirlist结果放入result变量中
endmacro()																#函数结束
#上一级目录
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)

#查找include目录下的所有*.hpp,*.h头文件,并路径列表保存到 INCLUDE_DIR_LIST 变量中
FIND_INCLUDE_DIR(INCLUDE_Src ${PARENT_DIR}/MyQt/Src )
FIND_INCLUDE_DIR(INCLUDE_MySrc "Src" )	
include_directories(${INCLUDE_Src})
include_directories(${INCLUDE_MySrc})
include_directories(${INCLUDE_MyTest})

include_directories("Src/XQPush/XQPush.h")

#第三方库
#add_definitions(-DTHIRDPARTY)  # 定义 THIRDPARTY 宏
#include_directories(${PARENT_DIR}/MyQt/ThirdPpartyLibrary)
#FIND_INCLUDE_DIR(INCLUDE_ThirdPpartySrc ${PARENT_DIR}/MyQt/ThirdPpartySrc )	
#include_directories(${INCLUDE_ThirdPpartySrc})
# 指定引用的外部库的搜索路径
#LINK_DIRECTORIES(${PARENT_DIR}/MyQt/ThirdPpartyLibrary/curl)
#LINK_DIRECTORIES(${PARENT_DIR}/MyQt/ThirdPpartyLibrary/zlib)
#LINK_DIRECTORIES(${PARENT_DIR}/MyQt/ThirdPpartyLibrary/xlnt)
 
#递归搜索目录下的源文件添加到变量中  "${PARENT_DIR}/MyQt/Src/*.cpp"
file(GLOB_RECURSE ServerCpp   "Src/Server/*.cpp")
file(GLOB_RECURSE ClientCpp   "Src/Client/*.cpp")
file(GLOB_RECURSE ResFile "${PARENT_DIR}/MyQt/resource/*.qrc" "resource/*.qrc")

find_package(QT NAMES Qt6  REQUIRED COMPONENTS Widgets Core5Compat network Gui Sql Charts)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Core5Compat network  Gui Sql Charts)
# 设置目录
if (CMAKE_BUILD_TYPE MATCHES "Debug")
   set(LIBRARY_DIRECTORY ${PARENT_DIR}/MyQt/bin/Debug)
else()
   set(LIBRARY_DIRECTORY ${PARENT_DIR}/MyQt/bin/Release)
endif()
find_library(XQT_LIBRARY NAMES xqt HINTS ${LIBRARY_DIRECTORY})

#设置图标
#aux_source_directory(. MY_SCOURCES)
#设置可执行文件的输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin)
#添加参加编译的文件
add_executable(XQPushServer
${ServerCpp}
"Src/Server/main.cpp"
"Src/XQPush/XQPush.cpp"
${ResFile}
"resource/icon.rc"
 )
target_link_libraries(XQPushServer ${XQT_LIBRARY}  Qt${QT_VERSION_MAJOR}::Widgets Qt6::Core5Compat Qt6::Network  Qt6::Gui Qt6::Sql Qt6::Charts)
#客户端
add_executable(XQPushClient
${ClientCpp}
"Src/Client/main.cpp"
"Src/XQPush/XQPush.cpp"
${ResFile}
"resource/icon.rc"
 )
target_link_libraries(XQPushClient ${XQT_LIBRARY}  Qt${QT_VERSION_MAJOR}::Widgets Qt6::Core5Compat Qt6::Network  Qt6::Gui Qt6::Sql Qt6::Charts)